<template>
  <div class="novice-page">
    <div class="header" v-if="noviceInfo">
      <div @click="$router.go(-1)" class="hd-left">
        <img src="../assets/images/back_b_icon.png" alt="" />
      </div>
      <div class="hd-md">
        <h4 v-if="!loading">{{noviceInfo.name}}</h4>
      </div>
      <div class="hd-right">
        <img src="../assets/images/black_more.png" alt="" />
      </div>
    </div>
    <div class="content" v-if="!loading">
      <div class="con-top" >
        <div class="logo-tips">
          <div class="img" :style="{'background-image':'url(https://pic.ecook.cn/web/'+noviceInfo.userimageid+'.jpg!wl280)'}">
            <!-- <img :src="'https://pic.ecook.cn/web/'+ noviceInfo.userimageid+'.jpg!wl280'" alt="" /> -->
          </div>
          <span class="little-hd">{{noviceInfo.usernickname}}</span>
        </div>
        <div class="ct-txt">{{noviceInfo.description || noviceInfo.name}}</div>
      </div>
      <div class="album">
        <div class="title-bg">
          <div class="list-title">
            <h4 class="title" v-if="noviceInfo">
              专辑菜谱&nbsp;&nbsp;<span>(共{{ noviceInfo.recipeCount }}道菜)</span>
            </h4>
            <div class="collect-album" @click="collect" v-if="!iscollect">
              <img src="../assets/images/增加.png" alt="" />
              <span>收藏专辑</span>
            </div>
            <div class="collect-album active" @click="collect" v-if="iscollect">
              <img src="../assets/images/kecheng_vipcard_icon.png" alt="" />
              <span>已收藏</span>
            </div>
          </div>
        </div>
        <div class="album-list"  v-if="noviceInfo">
        <van-empty v-if="noviceInfo.recipeList.length==0" description="目前没有内容" />
          <van-pull-refresh
            v-model="isLoading"
            @refresh="onRefresh"
            :head-height="50"
            :animation-duration="500"
          >
          <template #pulling>
            <span class="fresh">下拉可以刷新</span>
            <span class="freshTime">上次更新 {{freshTime}}</span>
          </template>
          <template #loosing>
            <span class="fresh">释放立即刷新</span>
            <span class="freshTime">上次更新 {{freshTime}}</span>
          </template>
          <template #loading>
            <van-loading size="18" type="spinner" >
            <span class="fresh">正在刷新</span>
            <span class="freshTime">上次更新 {{freshTime}}</span>
            </van-loading>
          </template>
          <template #success>
            <span class="fresh">刷新完成</span>
            <span class="freshTime">上次更新 {{freshTime}}</span>
          </template>
            <router-link
              :to="'/detail?id=' + item.id"
              tag="div"
              class="album-list-item"
              v-for="(item) in noviceInfo.recipeList"
              :key="item.id"
            >  
              <div
                class="album-img"
                :style="{ 'background-image': 'url(https://pic.ecook.cn/web/'+item.imageid+'.jpg!wl280)' }"
              ></div>
              <div class="album-txt">
                <h4 class="c-name one-txt-cut">{{ item.name }}</h4>
                <div class="p-info">
                  <img :src="'https://pic.ecook.cn/web/'+ item.authorimageid+'.jpg!wl280'" alt=""  v-lazy="'https://pic.ecook.cn/web/'+ item.authorimageid+'.jpg!wl280'"/>
                  <span class="p-name">{{ item.authorname }}</span>
                </div>
                <p class="caipu-info line-ellipsis" v-if="item.description!=''">
                  {{ item.description }}
                </p>
                <p class="caipu-info line-ellipsis" v-else>
                  暂无简介
                </p>
              </div>
            </router-link>
          </van-pull-refresh>
        </div>
      </div>
    </div>
    <van-loading size="24px" class="loading" v-if="loading" type="spinner">正在拼命加载中...</van-loading>
  </div>
</template>

<script>
import {getCollectionSortDetailData} from '../api/home'
export default {
  data() {
    return {
      noviceInfo: null,
      isLoading: false,
      freshTime:null,
      loading:true,
      iscollect:false
    };
  },
  methods: {
    //获取数据
    getNoviceDataFun(id) {
      this.isLoading = true
      getCollectionSortDetailData({id}).then(data=>{
        console.log(data);
        if (data.details) {
          setTimeout(() => {
            this.isLoading = false
            this.loading = false
          }, 800);
        }
        this.noviceInfo=data.details

        //收藏
        let ablumlist = JSON.parse(window.localStorage.getItem("ablumCollect"))
        if (ablumlist) {
          let index = ablumlist.findIndex(item=>item.id==this.$route.query.id)
          if (index!=-1) {
            this.iscollect = true
          }
        }
      })
    },
    onRefresh() {
      this.getNoviceDataFun(this.$route.query.id);
      this.getCurrentDay()
    },
    getCurrentDay(){
      let t = new Date();
      let m = t.getMonth()+1
      let d = t.getDate()
      let h = t.getHours()
      let min = t.getMinutes()<10?'0'+t.getMinutes():t.getMinutes()
      this.freshTime = `${m}-${d} ${h}:${min}`
    },
    //收藏
    collect(){
      this.iscollect = !this.iscollect
      let ablumlist = JSON.parse(window.localStorage.getItem("ablumCollect"))
      ablumlist = ablumlist==null?[]:ablumlist
      let index = ablumlist.findIndex(item=>item.id==this.$route.query.id)
      if (this.iscollect) {
        if (index==-1) {
          ablumlist.push({
            id:this.$route.query.id,
            usernickname:this.noviceInfo.usernickname,
            imageid:this.noviceInfo.imageid,
            recipeCount:this.noviceInfo.recipeCount,
            name:this.noviceInfo.name
          })
        }
      }else{
        ablumlist.splice(index,1)
      }
      window.localStorage.setItem("ablumCollect",JSON.stringify(ablumlist))
    }
  },
  created() {
    this.getNoviceDataFun(this.$route.query.id);
    this.getCurrentDay()
  },
};
</script>

<style lang="less" scoped>
.novice-page {
  width: 100%;
  height: 100%;
  overflow: hidden;

  .header {
    height: 50px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .hd-left {
      width: 13px;
      height: 20px;
      margin-left: 15px;
    }

    .hd-md {
      max-width: 41.6%;

      & > h4 {
        font-size: 16px;
        font-weight: bold;
        letter-spacing: 0.8px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
    }

    .hd-right {
      width: 25px;
      margin-right: 15px;
    }
  }

  .content {
    height: 100%;
    overflow-y: scroll;

    .con-top {
      height: 60px;
      padding: 10px 20px;
      display: flex;
      justify-content: center;
      flex-direction: column;
      margin-bottom: 5px;

      .logo-tips {
        height: 25px;
        display: flex;
        align-items: center;

        .img {
          width: 25px;
          height: 25px;
          border-radius: 50%;
          overflow: hidden;
          margin-right: 10px;
          background-position: center center;
          background-repeat: no-repeat;
          background-size: cover;
        }

        .little-hd {
          color: #666;
          font-size: 14px;
          font-weight: bold;
        }
      }

      .ct-txt {
        height: 35px;
        margin-top: 5px;
        line-height: 35px;
        color: #666666;
        font-weight: bold;
        font-size: 15px;
      }
    }

    .album {
      .title-bg {
        background-color: #fff;
        position: sticky;
        top: 0px;
        z-index: 10;

        .list-title {
          height: 60px;
          padding: 10px 20px 0 20px;
          background-color: #f6f6f6;
          border-top-left-radius: 12px;
          border-top-right-radius: 12px;
          overflow: hidden;
          display: flex;
          justify-content: space-between;
          align-items: center;

          .title {
            font-size: 16px;
            font-weight: bold;

            & > span {
              font-size: 13px;
              color: #666666;
              letter-spacing: 0.5px;
            }
          }

          .collect-album {
            display: flex;
            align-items: center;
            font-size: 13px;
            color: #fff;
            padding: 5px 10px;
            background-color: #fdc002;
            border-radius: 15px;

            & > img {
              width: 15px;
              height: 15px;
              display: block;
              margin-right: 5px;
            }
          }
          .active{
            background-color: #d8d6d6;
            color: #666;
          }
        }
      }

      .album-list {
        width: 100%;
        box-sizing: border-box;
        height: 100%;
        padding: 0 20px;
        padding-bottom: 50px;
        background-color: #f6f6f6;

        .album-list-item {
          width: 100%;
          height: 130px;
          background-color: #fff;
          padding: 8px;
          box-sizing: border-box;
          border-radius: 12px;
          display: flex;
          margin-bottom: 15px;

          .album-img {
            flex: 0 0 110px;
            height: 100%;
            border-radius: 8px;
            overflow: hidden;
            background-image: url(../assets/images/257457957.jpg);
            background-repeat: no-repeat;
            background-size: cover;
            background-position: center center;
          }

          .album-txt {
            width: 200px;
            margin-left: 10px;

            .c-name {
              font-size: 16px;
              font-weight: bold;
              margin: 10px 0;
            }

            .p-info {
              margin: 15px 0 8px 0;
              display: flex;
              align-items: center;

              & > img {
                width: 18px;
                height: 18px;
                display: block;
                border-radius: 50%;
                margin-right: 10px;
              }

              .p-name {
                font-size: 13px;
              }
            }

            .caipu-info {
              font-size: 13px;
              line-height: 20px;
              letter-spacing: 0.8px;
            }
          }
        }
      }
    }

    &::-webkit-scrollbar {
      display: none;
    }
  }
  .fresh{
    margin: 0;
    padding: 0;
    font-size: 14px;
    margin-right: 10px;
    margin-top: 5px;
    color: #000;
  }
  .freshTime{
    font-size: 13px;
  }
}
.loading {
  position: fixed;
  top: 50px;
  left: 0;
  right: 0;
  bottom: 100px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
}
::v-deep .van-empty {
   height: 100%;
}
</style>